package com.example.android.utils

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

/**
 * @ClassName MyRecyclerViewAdapter
 * @Author Ryan Kamanri
 * @Date 2022/6/1
 * @Description RecyclerViewAdapter封装
 **/
abstract class MyRecyclerViewAdapter<TModel, TViewHolder : RecyclerView.ViewHolder?>(val modelList: ArrayList<TModel>): RecyclerView.Adapter<TViewHolder>() {

    private var _OnItemClickListener: (model: TModel, view: View) -> Unit = {_, _ -> }

    fun SetOnItemClickListener(onItemClickListener: (model: TModel, view: View) -> Unit) {
        _OnItemClickListener = onItemClickListener
    }

    fun CreateView(parent: ViewGroup, resId: Int): View {
        val view = LayoutInflater.from(parent.context).inflate(resId, parent, false)
        view.setOnClickListener {
            val position = parent.indexOfChild(it)
            _OnItemClickListener(modelList[position], it)
        }

        return view
    }

    override fun getItemCount(): Int {
        return modelList.size
    }
}